﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EstateTrackerObjects.Enumerations;
using System.Data;
using System.Data.SqlClient;

namespace EstateTrackerObjects
{
    public class BaseEstateUpgrade
    {
        public int ID;
        public string name;
        public int InitialCost;
        public int MonthlyUpkeep;
        public float BaseIncome;
        public int BuildingUnitCost;
        public EstateType Type;
        public string Description;
        public bool RequiresSpecificType;
        //public string SpecificType;

        public BaseEstateUpgrade()
        {

        }

        public void load(int id)
        {
            string sql = "SELECT * FROM EstateUpgrades WHERE id=" + id;
            using (SqlConnection sConn = new SqlConnection(EstateTrackerObjects.Properties.Settings.Default.BeronisDBConnectionStringLive))
            {
                SqlCommand sCommand = new SqlCommand(sql, sConn);
                sConn.Open();

                SqlDataReader sReader = sCommand.ExecuteReader();
                if (sReader.Read())
                {
                    ID = (int)sReader["ID"];
                    name = (string)sReader["Name"];
                    InitialCost = (int)sReader["InitialCost"];
                    MonthlyUpkeep = (int)sReader["MonthlyUpkeep"];
                    BaseIncome = (float)sReader["BaseIncome"];
                    BuildingUnitCost = (int)sReader["BuildingUnitCost"];
                    Type = (EstateType)sReader["EstateType"];
                    Description = (string)sReader["Description"];
                    RequiresSpecificType = (bool)sReader["RequiresSpecificType"];
                    //SpecificType = (string)sReader["SpecificType"];
                }
                sReader.Close();
            }
        }

        public void save()
        {
//            string sqlUpdate = @"UPDATE EstateUpgrades SET Name=@Name, InitialCost=@InitialCost, MonthlyUpkeep=@MonthlyUpkeep, BaseIncome=@BaseIncome
//                                    , BuildingUnitCost=@BuildingUnitCost, EstateType=@EstateType, RequiresSpecificType=@RequiresSpecificType
//                                    , SpecificType=@SpecificType Description=@Description WHERE ID=@ID";
//            string sqlInsert = @"INSERT INTO EstateUpgrades (Name, InitialCost, MonthlyUpkeep, BaseIncome, BuildingUnitCost, EstateType, RequiresSpecificType
//                                    , SpecificType, Description) VALUES (@Name, @InitialCost, @MonthlyUpkeep, @BaseIncome, @BuildingUnitCost, @EstateType
//                                    , @RequiresSpecificType, @SpecificType, @Description);SELECT CAST(scope_identity() AS int)";
            string sqlUpdate = @"UPDATE EstateUpgrades SET Name=@Name, InitialCost=@InitialCost, MonthlyUpkeep=@MonthlyUpkeep, BaseIncome=@BaseIncome
                                    , BuildingUnitCost=@BuildingUnitCost, EstateType=@EstateType, RequiresSpecificType=@RequiresSpecificType
                                    , Description=@Description WHERE ID=@ID";
            string sqlInsert = @"INSERT INTO EstateUpgrades (Name, InitialCost, MonthlyUpkeep, BaseIncome, BuildingUnitCost, EstateType, RequiresSpecificType
                                    , Description) VALUES (@Name, @InitialCost, @MonthlyUpkeep, @BaseIncome, @BuildingUnitCost, @EstateType
                                    , @RequiresSpecificType,  @Description);SELECT CAST(scope_identity() AS int)";
            using (SqlConnection conn = new SqlConnection(EstateTrackerObjects.Properties.Settings.Default.BeronisDBConnectionString))
            {
                SqlCommand command = new SqlCommand();
                command.Parameters.AddWithValue("Name", name);
                command.Parameters.AddWithValue("InitialCost", InitialCost);
                command.Parameters.AddWithValue("MonthlyUpkeep", MonthlyUpkeep);
                command.Parameters.AddWithValue("BaseIncome", BaseIncome);
                command.Parameters.AddWithValue("BuildingUnitCost", BuildingUnitCost);
                command.Parameters.AddWithValue("EstateType", Type);
                command.Parameters.AddWithValue("Description", Description);
                command.Parameters.AddWithValue("RequiresSpecificType", RequiresSpecificType);
                //command.Parameters.AddWithValue("SpecificType", SpecificType);
                if (ID == 0) //Perform Insert
                {
                    command.CommandText = sqlInsert;
                    ID = (int)command.ExecuteScalar();
                }
                else // Perform Update
                {
                    command.Parameters.AddWithValue("ID", ID);
                    command.CommandText = sqlUpdate;
                    command.ExecuteNonQuery();
                }

            }
  
        }
    }
}
